package com.lhkj.ct.quartz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.lhkj.ct.quartz.mapper.TblJobLogMapper;
import com.lhkj.ct.quartz.model.entity.TblJobLog;
import com.lhkj.ct.quartz.service.SysJobLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Objects;

/**
 * <p>
 * 定时任务调度日志表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2023-09-23
 */
@Service
public class SysJobLogServiceImpl implements SysJobLogService {

    @Resource
    private TblJobLogMapper jobLogMapper;

    @Override
    public boolean save(TblJobLog entity) {
        return SqlHelper.retBool(jobLogMapper.insert(entity));
    }

    @Override
    public TblJobLog selectById(String jobLogId) {
        return jobLogMapper.selectById(jobLogId);
    }

    @Override
    public IPage<TblJobLog> selectJobPage(TblJobLog jobLog) {
        LambdaQueryWrapper<TblJobLog> queryWrapper = new LambdaQueryWrapper<TblJobLog>()
                .eq(StringUtils.isNotBlank(jobLog.getJobGroup()), TblJobLog::getJobGroup, jobLog.getJobGroup())
                .eq(Objects.nonNull(jobLog.getStatus()), TblJobLog::getStatus, jobLog.getStatus())
                .like(StringUtils.isNotBlank(jobLog.getJobName()), TblJobLog::getJobName, jobLog.getJobName());
        if (null != jobLog.getBeginTime() && null != jobLog.getEndTime()) {
            queryWrapper.between(TblJobLog::getCreateTime, jobLog.getBeginTime(), jobLog.getEndTime());
        }
        return jobLogMapper.selectPage(generatePage(jobLog), queryWrapper);
    }

    @Override
    public boolean removeByIds(Collection<?> list) {
        if (CollectionUtils.isEmpty(list)) {
            return false;
        }
        return SqlHelper.retBool(jobLogMapper.deleteBatchIds(list));
    }

    @Override
    public void clear() {
        jobLogMapper.clear();
    }
}
